﻿package sprites {
	import flash.display.*;
	import org.flixel.*;

	/**
	 * ...
	 * @author test
	 */
	public class Weapon extends FlxEmitter {
		public var wballs:Array;
		public var wball_group:FlxGroup;
		public var __shooter:FlxSprite;
		public var _energy:uint;
		public var _shoot_delay:Number;
		private var _delay:Number;
		public var dir:uint;
		public var _speed:Number;
		public var _range:Number;
		public var dirA:Array;
		public var _menergy:uint;
		public var _angle:Number;



		override public function update():void {
			super.update();

		}

		public function Weapon(_shooter:FlxSprite, number:uint = 100, size:uint = 5, delay:Number = 0.05, bColor:Number = 0xff0000, speed:Number = 100, range:Number = 50, menergy:uint = 100){
			super();

			wballs = new Array();
			var circle:Shape = new Shape();
			var bmp:Bitmap = new Bitmap();

			_menergy = new uint(menergy);
			_energy = new uint(_menergy);
			_shoot_delay = new Number(0);
			_delay = delay;
			__shooter = _shooter;
			_speed = speed;
			_range = range;
			_angle = 0;
			gravity = 0;

			dirA = new Array();
			dirA[FlxSprite.UP] = new FlxPoint(0, -speed);
			dirA[FlxSprite.DOWN] = new FlxPoint(0, speed);
			dirA[FlxSprite.LEFT] = new FlxPoint(-speed, 0);
			dirA[FlxSprite.RIGHT] = new FlxPoint(speed, 0);
			setDir(FlxSprite.UP);

			for (var i:Number = 0; i < number; i++){
				var wball:Bullet = new Bullet(this, 0, 0);
				//wball.origin = new FlxPoint(0, 0);
				var rands:Number = size * Math.random() / 10;
				wball.scale = new FlxPoint(rands, rands);
				wballs.push(wball);
				this.add(wballs[i]);
			}
			wball_group = new FlxGroup();
			wball_group.members = wballs;
		}

		/*
		   static public function handleHit(particle:FlxObject, obj:FlxObject):void {
		   trace("XD");
		   }
		 */

		public function recover(e:uint):void {
			_energy = (_energy + e > this._menergy ? this._menergy : _energy + e);
		}

		protected function calcRange():Number {
			return -_range + (FlxU.random() * _range * 2);
		}

		public function setDir(_dir:Number):void {
			this.dir = _dir;
		}

		public function shoot():void {
			if (_energy > 0 && _shoot_delay <= 0){
				/*
				   var xs:Number = dirA[dir].x == 0 ? calcRange() : dirA[dir].x;
				   var ys:Number = dirA[dir].y == 0 ? calcRange() : dirA[dir].y;
				 */
				angle %= 360;
				var r:Number = calcRange();
				var _icos:Number = Utils.icos(angle);
				var _isin:Number = Utils.isin(angle);
				var xs:Number = _speed * _icos + r * _isin;
				var ys:Number = _speed * _isin + r * _icos;
				setXSpeed(xs, xs);
				setYSpeed(ys, ys);

				start(true, 5, 1);
				_energy--;
				_shoot_delay = _delay;
				trace("shoot", _energy);
			}
		}
	}

}